<template>
    <div class="mainDiv" style="padding: 20px;box-sizing: border-box;">
        <el-table :data="list" :stripe="true" @row-click="selectRow" @row-dblclick="submit" height="100%">
            <el-table-column prop="gradeid" label="权限等级"></el-table-column>
            <el-table-column prop="remarks" label="等级描述" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="edittime" label="更改时间"></el-table-column>
            <el-table-column label="选择" prop="selected">
                <template slot-scope="scope">
                    <el-switch @change="geibian(scope.row)" v-model="scope.row.selected" on-color="#13ce66" off-color="#ff4949">
                    </el-switch>
                </template>
            </el-table-column>
        </el-table>
        <div style="text-align: right">
            <el-button type="danger" icon="close" @click="close">取消关闭</el-button>
            <el-button type="success" icon="check" @click="submit">确定选择</el-button>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'groupSelectgrade',
  data () {
    return {
      list: [],
      id: '',
      rid: '',
      seleid: 0
    }
  },
  mounted () {
    let request = C.getDatas()
    if (request) {
      this.id = request.id
      this.rid = request.rid
    }
    this.getList()
  },
  methods: {
    geibian: function (row) {
      this.list.forEach((v) => {
        v.selected = false
      })
      row.selected = true
      this.seleid = row.id
    },
    selectRow: function (row, event, column) {
      this.list.forEach((v) => {
        v.selected = false
      })
      row.selected = true
      this.seleid = row.id
    },
    submit: async function () {
      if (!this.seleid) {
        C.error('请设置帐号的权限等级')
        return false
      }
      let quan = {
        id: this.id,
        groupid: this.seleid
      }
      let res = await C.ajax('/organization/group/setgrage', quan, 'post')
      if (res) C.closeBrowserWindow(res)
    },
    close: function () {
      C.closewin()
    },
    getList: async function () {
      let data = await C.ajax('/organization/group/getgradelist', {}, 'post')
      if (data) {
        data.forEach((v) => {
          if (v.id == this.rid) {
            v.selected = true
          } else {
            v.selected = false
          }
        })
        this.list = data
      }
    }
  }
}
</script>